import {reqHomeArtical} from '../api/home'
// import {reqGetArticle, reqFollowArticle, reqUnFollowArticle} from '../api/article'
// import marked from 'marked'
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-dark.css'
const  md = new MarkdownIt({
  highlight: function (str, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return '<pre class="hljs"><code>' +
               hljs.highlight(lang, str, true).value +
               '</code></pre>';
      } catch (__) {}
    }

    return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
  }
})
// 获取文章列表
export const getArticle = {
  data: () => {
    return {
      article:{
        tag: null,
        author: null,
        favorited: null,
      },
      currentPage: 1,
      pageSize: 10,
      // 文章总数量
      totalArticle: 0,
      articleList: [],
    }
  },
  methods: {
    // 获取文章数据
    async getUserArtical() {
      const result = await reqHomeArtical(this.article, this.pageSize, this.currentPage)
      // console.log(result)
      
      this.articleList = this.changeData(result.articles)
      // 文章总数量
      this.totalArticle = result.articlesCount;
    },

    // 将markdown转换为HTML
    changeData(data){
      let result = data.map(item => {
        item.body = md.render(item.body)
        return item
      })
      return result
    },
  },
}

// 页码着色和重新发送请求
export const changePage = {
  data: () => {
    return {
      currentPage: 1,
    }
  },
  methods: {
    pageActive(i){
      this.currentPage = i
    },
  },
}

